package org.iabako.client.ui.form.extranet;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.tools.Messages;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.*;
import org.iabako.client.ui.layout.widget.dialogbox.IabakoDialogBox;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.user.User;
import org.iabako.shared.iservice.user.EmailVerificationServiceAsync;
import org.iabako.shared.tools.GenericTools;

import java.util.List;

/**
 * Created by jose on 06/01/15.
 */
public abstract class EmailVerificationForm extends AbstractForm {

    protected final EmailVerificationServiceAsync emailVerificationServiceAsync = EmailVerificationServiceAsync.Util.getInstance();

    @UiField
    public InputTextEmail email;
    @UiField
    public LabelText verificationText;
    @UiField
    public LabelText verificationHelp;
    @UiField
    public InputText keyCode;
    @UiField
    public HTMLPanel emailPanel;
    @UiField
    public HTMLPanel emailVerificationPanel;
    @UiField
    public Button goAheadEmail;
    @UiField
    public Button goAheadVerification;

    protected boolean userExistsVerification;

    protected void goAheadEmailHandler() {
        goAheadEmail.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                if (GenericTools.isEmpty(email.getValue())) {
                    email.addErrorMessage(email, true, "validation_form_mandatory_red_fields_error");
                    return;
                }
                if (!email.isValid()) {
                    email.addErrorMessage(email, true, "validation_email_format_error");
                    return;
                }

                emailVerificationServiceAsync.sendKeyCode(email.getValue(), userExistsVerification, new IabakoAsyncCallback<User>() {
                    public void onSuccess(User result) {

                        customSendKeyCodeAction(result);

                        super.onSuccess(result);
                    }
                });
            }
        });
    }

    protected abstract void customSendKeyCodeAction(User result);


    protected void initializeEmailVerificationPanel(String activationTextLabelKey, String activationHelpLabelKey) {
        verificationText.setHtmlLabel(Messages.getLabel(activationTextLabelKey, email.getValue()));

        verificationHelp.setHtmlLabel(Messages.getLabel(activationHelpLabelKey, email.getValue()));

        verificationHelp.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                String emailProvider = email.getValue().substring(email.getValue().indexOf("@") + 1);
                String text = Messages.getLabel("email_verification_no_email_text_help", email.getValue(), emailProvider);

                IabakoDialogBox iabakoDialogBox = new IabakoDialogBox("sign_up_help_no_validation_email_title", text);
                iabakoDialogBox.setAvoidClosePopupCall(true);

                NavigationController.getInstance().show(iabakoDialogBox);
            }
        });
    }

    protected void goAheadVerificationHandler() {
        goAheadVerification.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                if (GenericTools.isEmpty(keyCode.getValue())) {
                    email.addErrorMessage(keyCode, true, "validation_form_mandatory_red_fields_error");
                    return;
                }

                emailVerificationServiceAsync.verifyKeyCode(email.getValue(), keyCode.getValue(), new IabakoAsyncCallback<Boolean>() {
                    public void onSuccess(Boolean result) {
                        customVerifyKeyCodeAction(result);

                        super.onSuccess(result);
                    }
                });
            }
        });
    }

    protected abstract void customVerifyKeyCodeAction(Boolean result);


    @Override
    protected List<IField> getMandatoryFields() {
        return null;
    }

    @Override
    protected AbstractEntity getEntity() {
        return null;
    }

    @Override
    public boolean validate() {
        return false;
    }
}
